package com.example.jbmsuppliers.sinopec.orderaddress;

import android.app.Activity;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.example.jbmsuppliers.R;
import com.example.jbmsuppliers.sinopec.activity.ActivityUse;
import com.example.jbmsuppliers.sinopec.net.NetAddRess;
import com.example.jbmsuppliers.sinopec.utlis.ExitAppUtlis;
import com.example.jbmsuppliers.sinopec.view.DialogLoading;
import com.jbm.jbmsupplier.util.ICertAddress;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

public class AddressEditActivity extends ActivityUse {

    @InjectView(R.id.linear01)
    LinearLayout linear01;
    @InjectView(R.id.linear02)
    LinearLayout linear02;
    @InjectView(R.id.button_bar)
    LinearLayout buttonBar;
    @InjectView(R.id.regisgood)
    TextView regisgood;
    private Spinner mCityCtrl = null, mRegionCtrl;
    private EditText mAddress = null;
    private TextView okButton = null, cannelButton = null;
    private EditText mContactsEdit = null, mMobileEdit = null;
    private List<String> mCityList = new ArrayList<String>();
    private List<String> mRegionList = new ArrayList<String>();
    private ArrayAdapter<String> mAdapter, mRegionAdapter;
    private String mStringCity = "天津市";
    private String mStringRegion = "河北区";
    private final int ADDRESSMESAGE = 0001;
    AddressEditActivity my = null;
    private String edits;
    DialogLoading dialogLoading;
    private final int EXIT = 0002;

    @Override
    protected int setContentView1() {
        return R.layout.activity_address_edit;
    }

    @Override
    protected void initView() {

        ExitAppUtlis.getInstance().addActivity(this);
        my = this;
        ButterKnife.inject(this);
        try {
            dialogLoading = new DialogLoading(this, "添加中...", true);
            this.mCityCtrl = (Spinner) this.findViewById(R.id.id_spinner_city);
            this.mRegionCtrl = (Spinner) this.findViewById(R.id.id_spinner_region);
            this.mAddress = (EditText) this.findViewById(R.id.id_edit_address);
            this.mContactsEdit = (EditText) this.findViewById(R.id.id_edit_contacts);
            this.mMobileEdit = (EditText) this.findViewById(R.id.id_edit_contacts_mobile);



            mCityList.add("天津市");
            mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, mCityList);
            mAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            this.mCityCtrl.setAdapter(mAdapter);
            this.mCityCtrl.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    mStringCity = mAdapter.getItem(position);
                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {
                    mStringCity = "天津市";
                }
            });

            mRegionList.add("和平区");
            mRegionList.add("河北区");
            mRegionList.add("河西区");
            mRegionList.add("南开区");
            mRegionList.add("红桥区");
            mRegionList.add("河东区");
            mRegionList.add("北辰区");
            mRegionList.add("津南区");
            mRegionList.add("西青区");
            mRegionList.add("东丽区");

            this.mRegionAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, mRegionList);
            mAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            this.mRegionCtrl.setAdapter(mRegionAdapter);
            this.mRegionCtrl.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    mStringRegion = mRegionAdapter.getItem(position);
                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {
                    mStringRegion = "河北区";
                }
            });

            this.okButton = (TextView) this.findViewById(R.id.id_edit_address_ok);
            if (okButton != null) {
                okButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        edits = mAddress.getText().toString();
                        String address = mStringCity + mStringRegion + edits;
                        String contacts = mContactsEdit.getText().toString();
                        String mobile = mMobileEdit.getText().toString();//电话
                        if (address.isEmpty() || address.length() < 6) {

                            hint("请输入正确的地址");
                        } else if (contacts.equals("")) {
                            hint("用户名不能为空");
                        } else if (mobile.equals("")) {
                            hint("手机号不能为空");
                        } else if (edits.equals("")) {
                            hint("内容不能为空");
                        } else {
                            if (mobile.length() != 11) {
                                hint("请核实您的手机号码");
                            } else {
                                NetAddRess netAddRess = new NetAddRess(mHandler);
                                netAddRess.AddAddressList(address, mobile,contacts, ADDRESSMESAGE);
                                if (dialogLoading != null) {
                                    dialogLoading.show();
                                }
                            }
                        }

                    }
                });
            }
            this.cannelButton = (TextView) this.findViewById(R.id.id_edit_address_cannel);
            if (cannelButton != null) {
                cannelButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        setResult(Activity.RESULT_CANCELED);
                        finish();
                    }
                });
            }
        } catch (Exception e) {
            String msg = e.getMessage();
        }
        if (getIntent().getStringExtra("add").equals("")){}else
        if (getIntent().getStringExtra("add").equals("add")) {
            linear01.setVisibility(View.GONE);
            linear02.setVisibility(View.GONE);
            buttonBar.setVisibility(View.GONE);
            regisgood.setVisibility(View.VISIBLE);
        }
    }
    private ICertAddress result;
    private Handler mHandler = new Handler() {
        public void handleMessage(Message paramMessage) {
            switch (paramMessage.what) {
                case ADDRESSMESAGE:
                    result = (ICertAddress) paramMessage.obj;
                    if (dialogLoading != null) {
                        dialogLoading.dismiss();
                    }
                    if (result.back) {
                        mHandler.sendEmptyMessageDelayed(EXIT, 2000);
                        Intent intent = new Intent();
                        intent.putExtra("address", result);
                        setResult(RESULT_OK, intent);
                        finish();
                    } else {
                            mHandler.sendEmptyMessageDelayed(EXIT, 2000);

                    }
                    break;
                case EXIT: {


                    break;
                }
            }

        }
    };


    @OnClick(R.id.regisgood)
    public void onClick() {
        edits = mAddress.getText().toString();
        if (edits.equals("")){
            hint("详细地址不能为空");
        }else {
        String address = mStringCity + mStringRegion + edits;
        Intent in=new Intent();
        in.putExtra("address",address);
        setResult(RESULT_OK,in);
        finish();}
    }
}
